package top.lixunda.ecommerce.server.good.api.entity.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableBiMap;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.api.condition.ILambdaQueryWrapperDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.OrderByDTO;
import top.lixunda.ecommerce.server.good.api.entity.db.SimpleReviewDetailDO;

import java.util.List;
import java.util.Map;

/**
 * 商品评论信息查询DTO
 *
 * @author lixunda
 * @version time:2020/3/11 17:17
 */
@ApiModel("商品评论信息查询DTO")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SimpleReviewQueryDTO extends OrderByDTO
        implements ILambdaQueryWrapperDTO<SimpleReviewDetailDO> {

    private List<Integer> id;

    private List<Integer> userId;

    private List<Integer> storeId;

    private List<Integer> goodId;

    private Integer scoreBegin;

    private Integer scoreEnd;

    private Integer praiseBegin;

    private Integer praiseEnd;

    /**
     * 排序字段映射
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private static final Map<String, SFunction<SimpleReviewDetailDO, ?>> MAP =
            new ImmutableBiMap.Builder<String, SFunction<SimpleReviewDetailDO, ?>>()
                    .put("id", SimpleReviewDetailDO::getId)
                    .put("userId", SimpleReviewDetailDO::getUserId)
                    .put("userName", SimpleReviewDetailDO::getUserName)
                    .put("storeId", SimpleReviewDetailDO::getStoreId)
                    .put("storeName", SimpleReviewDetailDO::getStoreName)
                    .put("goodId", SimpleReviewDetailDO::getGoodId)
                    .put("goodName", SimpleReviewDetailDO::getGoodName)
                    .put("score", SimpleReviewDetailDO::getScore)
                    .put("content", SimpleReviewDetailDO::getContent)
                    .put("time", SimpleReviewDetailDO::getTime)
                    .put("removeTime", SimpleReviewDetailDO::getRemoveTime)
                    .put("removeUserId", SimpleReviewDetailDO::getRemoveUserId)
                    .put("removeUserName", SimpleReviewDetailDO::getRemoveUserName)
                    .put("praise", SimpleReviewDetailDO::getPraise)
                    .build();

    /**
     * 将当前用于查询的DTO中的查询数据转换为 LambdaQuerySqlWrapper
     *
     * @return 条件构造器 LambdaQuerySqlWrapper
     */
    @Override
    public LambdaQueryWrapper<SimpleReviewDetailDO> toWrapper() {
        LambdaQueryWrapper<SimpleReviewDetailDO> wrapper = new LambdaQueryWrapper<>();
        if (ObjectValidators.isNotEmpty(this.getId())) {
            wrapper.and(innerWrapper ->
                    this.getId().forEach(item ->
                            innerWrapper.or().eq(SimpleReviewDetailDO::getId, item)));
        }
        if (ObjectValidators.isNotEmpty(this.getGoodId())) {
            wrapper.and(innerWrapper ->
                    this.getGoodId().forEach(item ->
                            innerWrapper.or().eq(SimpleReviewDetailDO::getGoodId, item)));
        }
        if (ObjectValidators.isNotEmpty(this.getStoreId())) {
            wrapper.and(innerWrapper ->
                    this.getStoreId().forEach(item ->
                            innerWrapper.or().eq(SimpleReviewDetailDO::getStoreId, item)));
        }
        if (ObjectValidators.isNotEmpty(this.getUserId())) {
            wrapper.and(innerWrapper ->
                    this.getUserId().forEach(item ->
                            innerWrapper.or().eq(SimpleReviewDetailDO::getUserId, item)));
        }
        if (ObjectValidators.isNotEmpty(this.getScoreBegin())) {
            wrapper.ge(SimpleReviewDetailDO::getScore, this.getScoreBegin());
        }
        if (ObjectValidators.isNotEmpty(this.getScoreEnd())) {
            wrapper.le(SimpleReviewDetailDO::getScore, this.getScoreEnd());
        }
        if (ObjectValidators.isNotEmpty(this.getPraiseBegin())) {
            wrapper.ge(SimpleReviewDetailDO::getPraise, this.getPraiseBegin());
        }
        if (ObjectValidators.isNotEmpty(this.getPraiseEnd())) {
            wrapper.le(SimpleReviewDetailDO::getPraise, this.getPraiseEnd());
        }
        dealWithOrderBy(wrapper, MAP);
        return wrapper;
    }
}
